package com.entfrm.biz.it.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entfrm.biz.it.entity.PropertyCategory;
import com.entfrm.biz.it.service.PropertyCategoryService;
import com.entfrm.core.log.annotation.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import com.entfrm.core.base.api.R;
import com.entfrm.core.base.util.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Arrays;

/**
 * @author kyc
 * @date 2020-08-07 14:07:33
 * @description 资产分类Controller
 */
@Api("资产分类管理")
@RestController
@AllArgsConstructor
@RequestMapping("/it/propertyCategory")
public class PropertyCategoryController {

    private final PropertyCategoryService propertyCategoryService;

    private QueryWrapper<PropertyCategory> getQueryWrapper(PropertyCategory propertyCategory) {
        return new QueryWrapper<PropertyCategory>()
                .eq(StrUtil.isNotBlank(propertyCategory.getName()), "name", propertyCategory.getName())
                .eq(StrUtil.isNotBlank(propertyCategory.getStatus()), "status", propertyCategory.getStatus())
                .between(StrUtil.isNotBlank(propertyCategory.getBeginTime()) && StrUtil.isNotBlank(propertyCategory.getEndTime()), "create_time", propertyCategory.getBeginTime(), propertyCategory.getEndTime())
                .orderByDesc("create_time");
    }

    @ApiOperation("资产分类列表")
    @PreAuthorize("@ps.hasPerm('propertyCategory_view')")
    @GetMapping("/list")
    public R list(PropertyCategory propertyCategory) {
        List<PropertyCategory> propertyCategoryList = propertyCategoryService.list(getQueryWrapper(propertyCategory));
        return R.ok(propertyCategoryList);
    }

    @ApiOperation("资产分类查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(propertyCategoryService.getById(id));
    }

    @OperLog("资产分类新增")
    @ApiOperation("资产分类新增")
    @PreAuthorize("@ps.hasPerm('propertyCategory_add')")
    @PostMapping("/save")
    public R save(@Validated @RequestBody PropertyCategory propertyCategory) {
        PropertyCategory info = null;
        if (0 != propertyCategory.getParentId()){
            info = propertyCategoryService.getById(propertyCategory.getParentId());
            propertyCategory.setAncestors(info.getAncestors()+","+propertyCategory.getParentId());
        }else {
            propertyCategory.setAncestors("0");
        }
        propertyCategoryService.save(propertyCategory);
        return R.ok();
    }

    @OperLog("资产分类修改")
    @ApiOperation("资产分类修改")
    @PreAuthorize("@ps.hasPerm('propertyCategory_edit')")
    @PutMapping("/update")
    public R update(@Validated @RequestBody PropertyCategory propertyCategory) {
        propertyCategoryService.updateById(propertyCategory);
        return R.ok();
    }


    @OperLog("资产分类删除")
    @ApiOperation("资产分类删除")
    @PreAuthorize("@ps.hasPerm('propertyCategory_del')")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable("id") Integer[] id) {
        return R.ok(propertyCategoryService.removeByIds(Arrays.asList(id)));
    }


    @GetMapping("/propertyCategoryTree")
    public R propertyCategoryTree() {
        List<PropertyCategory> propertyCategoryList = propertyCategoryService.list(new QueryWrapper<PropertyCategory>().orderByAsc("sort"));
        return R.ok(propertyCategoryService.buildTree(propertyCategoryList, 0));
    }

    @PreAuthorize("@ps.hasPerm('propertyCategory_export')")
    @GetMapping("/export")
    public R export(PropertyCategory propertyCategory) {
        List<PropertyCategory> list = propertyCategoryService.list(getQueryWrapper(propertyCategory));
        ExcelUtil<PropertyCategory> util = new ExcelUtil<PropertyCategory>(PropertyCategory.class);
        return util.exportExcel(list, "资产分类数据");
    }
}
